package com.sheuni.demodatabinding;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.sheuni.demodatabinding.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private Presenter presenter = new Presenter();
    private User user = new User();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        binding.setUser(user);
        binding.setPresenter(presenter);
    }

    public class Presenter {

        public void methodReferencesOnClick(View view) {
            Toast.makeText(MainActivity.this, "方法引用：" + user, Toast.LENGTH_LONG).show();
        }

        // 监听器绑定： 方式的事件监听 方法参数可以灵活使用
        public void listenerBindingOnClick(View view, User user) {
            Toast.makeText(MainActivity.this, "监听器绑定：" + user, Toast.LENGTH_LONG).show();
        }

        //方法引用：方式的事件监听 方法的形参一定要一致
        public void onTextChanged(CharSequence text,int start,int lengthBefore,int lengthAfter) {
            user.setDescription(text.toString());
            binding.description.setText(text);
        }
    }
}
